package demo1.servlet.all;

import com.alibaba.fastjson.JSON;
import demo1.pojo.users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/getCookie")
public class getCookie extends HttpServlet {//缓存cookie
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                // 打印每个Cookie的名字和值
                //System.out.println("Cookie Name: " + cookie.getName() + ", Value: " + cookie.getValue());
            }
            users users = new users();
            users.setUserName(cookies[2].getValue());
            users.setUserPwd(cookies[1].getValue());
            String jsonString = JSON.toJSONString(users);
            resp.setContentType("text/json;charset=utf-8");
            resp.getWriter().write(jsonString);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
